<?
#####################################################
#  COPYRIGHT NOTICE
#####################################################
# In the following statements, the term "this 
# program" refers to all files associated with the
# execution and distribution of Adtrackz.
#
# This Program is Copyright 2004 Jonah Klimack.
# All Rights Reserved. 
#
# Selling the code for this program, modifying 
# and/or redistributing the code for this program
# in part or in whole over the Internet or in any 
# other medium is expressly forbidden. Violators
# will be prosecuted to the fullest extent of the law
# Copyright and header information may not be
# modified.
#
# This program is distributed "as is" and without 
# warranty of any kind, either express or implied.
# In no event shall the liability of Jonah Klimack 
# for any damages, losses and/or causes of 
# action exceed the total amount paid by the 
# user for this software.		
#####################################################
# DO NOT MODIFY ANYTHING IN THIS FILE
#####################################################


require "header.php";

?>

<p class="small" align="left">
Cloak > Add New
</p>
<p class="heading">
Cloak Your Campaigns
</p>


<u>Create Redirect</u>

<p>
You can shorten your tracking urls by using the cloaking feature. For example, instead of <i>http://yourdomain.com/go.php?c=campaign</i> you can have <i>http://yourdomain.com/go/</i>.
</p><p>
You can also hide your affiliate id by using the cloak or encrypt option. You can enter both encrypt and sticky, all this does is encrypt your page while at the same time keeping your cloaked url in the browser
 address bar. Or you can choose no option for a simple html redirect file.
</p>


<?


if ($submit)
{
	if (!$filename)
		error("Please enter a filename.");
	elseif($encrypt && !$merchant_url)
		error("You must enter a merchant url if you choose to encrypt your redirect page.");
	elseif(mysql_fetch_row(mysql_query("select * from adtrackz_redirects where filename='$ftpfile'")))
		error("You have already created $wwwfile. Please delete it by clicking on View/Edit under the cloak menu and try again.");
	elseif($select_campaign && $select_splittest)
		error("Please choose either a campaign or a split test. You can't select both.");
	elseif(!$select_campaign && !$select_splittest)
		error("Please choose either a campaign or a split test.");
	elseif($select_campaign && !$cID)
		error("You must select a campaign.");
	elseif($select_splittest && !$st_ID)
		error("You must select a split test.");
	else
	{
		//CREATE CONTENT OF FILE
		//cloaker (keep url on top) - simply a framed page, visitors can view source
		//encrypt - a framed page, but when visitor views source, they get hex code
		//normal - a normal html redirect page

		if ($select_campaign)
		{
			$campaign_name = mysql_fetch_row(mysql_query("select adcode from adtrackz_campaigns where ID='$cID'"));
			$tracking_url = "http://".$domain."/".$udirectory."/go.php?c=$campaign_name[0]&s=$sub"; 
		}
		elseif ($select_splittest)
		{
			$st_name = mysql_fetch_row(mysql_query("select name from adtrackz_split_test_groups where ID='$st_ID'"));
			$tracking_url = "http://".$domain."/".$udirectory."/st.php?s=$st_name[0]"; 
		}

		//need ftpfile, wwwfile for saving to mysql, and display, respectively
		//need wwwfile_embed for cloak && encrypt
		if ($directory)
		{
			$ftpfile = $basedir."/".$directory."/".$filename.$extension;
			$wwwfile = "http://".$domain."/".$directory."/".$filename.$extension;
			$wwwfile_embed = "http://".$domain."/".$directory."/".$filename."_embed".$extension;
		}
		else 
		{
			$ftpfile = $basedir."/".$filename.$extension;
			$wwwfile = "http://".$domain."/".$filename.$extension;
			$wwwfile_embed = "http://".$domain."/".$filename."_embed".$extension;
		}

		if ($sticky && $encrypt)
		{
			$contents = '
			<HTML>
			<HEAD>
				<TITLE>'.$title.'</TITLE>
			</HEAD>
			<frameset border=0 frameBorder=0 rows=100%,* marginbottom=0 margintop=0 marginright=0 marginleft=0>
				<FRAME name=_main src="'.$wwwfile_embed.'" target="_main"  scrolling="yes" noresize frameborder="no" framespacing="0">
				<frame border=0 noresize>
				<NOFRAMES>
					<body>
						<p>This page uses frames, but your browser doesn\'t support them.</p>
						<p><a href="'.$tracking_url.'">Click Here to Continue...</a></p>
					</body>
				</NOFRAMES>
			</FRAMESET>
			</HTML>
			';
			//echo "<form><textarea name=a rows=10 cols=30>$contents</textarea></form>";

			//EMBEDED SOURCE
			$src = "<embed src=\"$tracking_url\" width=\"2\" height=\"2\"></embed><meta http-equiv=\"Refresh\" content=\"0;url=$merchant_url\">";

			//echo "<form><textarea name=b rows=10 cols=30>$src</textarea></form>";

			//ord the src
			for ($i=0; $i < strlen($src); $i++)
			   $src2 .= dechex(ord(substr($src,$i))); 
			$src2 = strtoupper($src2);

			//echo "<form><textarea name=b rows=10 cols=30>$src2</textarea></form>";

			$contents_embed = '
			<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
			<HTML>
			<HEAD>
			<TITLE> One Moment Please... </TITLE>
			</HEAD>
			<BODY>
			<SCRIPT language=Javascript>	q="'.$src2.'";r="683D22223B666F72286A323D303B6A323C712E6C656E6774683B6A322B2B297B6966286A3225323D3D3029682B3D2225223B682B3D712E636861724174286A32293B7D646F63756D656E742E777269746528756E657363617065286829293B683D22223B";eval(unescape("%67%3D%22%22%3B%66%6F%72%28%6A%3D%30%3B%6A%3C%72%2E%6C%65%6E%67%74%68%3B%6A%2B%2B%29%7B%69%66%28%6A%25%32%3D%3D%30%29%67%2B%3D%22%25%22%3B%67%2B%3D%72%2E%63%68%61%72%41%74%28%6A%29%3B%7D%65%76%61%6C%28%75%6E%65%73%63%61%70%65%28%67%29%29%3B%67%3D%22%22%3B"));
			</script>
			</BODY>
			</HTML>
			';

			//echo "<form><textarea name=a rows=10 cols=30>$contents</textarea></form>";

		}
		elseif ($sticky)
		{
			//create framed page? or...............
			$contents = '
			<HTML>
			<HEAD>
				<TITLE>'.$title.'</TITLE>
			</HEAD>
			<frameset border=0 frameBorder=0 rows=100%,* marginbottom=0 margintop=0 marginright=0 marginleft=0>
				<FRAME name=_main src="'.$tracking_url.'" target="_main"  scrolling="yes" noresize frameborder="no" framespacing="0">
				<frame border=0 noresize>
				<NOFRAMES>
					<body>
						<p>This page uses frames, but your browser doesn\'t support them.</p>
						<p><a href="'.$tracking_url.'">Click Here to Continue...</a></p>
					</body>
				</NOFRAMES>
			</FRAMESET>
			</HTML>
			';
			//echo "<form><textarea name=a rows=10 cols=30>$contents</textarea></form>";
		}
		elseif ($encrypt)
		{
			//purpose: to protect affiliate links

			$src = "<embed src=\"$tracking_url\" width=\"2\" height=\"2\"></embed><meta http-equiv=\"Refresh\" content=\"0;url=$merchant_url\">";

			//echo "<form><textarea name=b rows=10 cols=30>$src</textarea></form>";

			//ord the src
			for ($i=0; $i < strlen($src); $i++)
			   $src2 .= dechex(ord(substr($src,$i))); 
			$src2 = strtoupper($src2);

			//echo "<form><textarea name=b rows=10 cols=30>$src2</textarea></form>";

			$contents = '
			<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
			<HTML>
			<HEAD>
			<TITLE> One Moment Please... </TITLE>
			</HEAD>
			<BODY>
			<SCRIPT language=Javascript>	q="'.$src2.'";r="683D22223B666F72286A323D303B6A323C712E6C656E6774683B6A322B2B297B6966286A3225323D3D3029682B3D2225223B682B3D712E636861724174286A32293B7D646F63756D656E742E777269746528756E657363617065286829293B683D22223B";eval(unescape("%67%3D%22%22%3B%66%6F%72%28%6A%3D%30%3B%6A%3C%72%2E%6C%65%6E%67%74%68%3B%6A%2B%2B%29%7B%69%66%28%6A%25%32%3D%3D%30%29%67%2B%3D%22%25%22%3B%67%2B%3D%72%2E%63%68%61%72%41%74%28%6A%29%3B%7D%65%76%61%6C%28%75%6E%65%73%63%61%70%65%28%67%29%29%3B%67%3D%22%22%3B"));
			</script>
			</BODY>
			</HTML>
			';

			//echo "<form><textarea name=a rows=10 cols=30>$contents</textarea></form>";
		}
		else //none was clicked
		{
			$contents = '
			<html>
			<head>
			<meta http-equiv="refresh"
			content="0; url='.$tracking_url.'">
			</head>
			</html>
			';

			//echo "<form><textarea name=a rows=10 cols=30>$contents</textarea></form>";
		}

		//variables
		$ftp_info = mysql_fetch_row(mysql_query("select ftp_user, ftp_pass from adtrackz_login"));
		echo mysql_error();
		$user = $ftp_info[0];
		$pass = $ftp_info[1];

		//the next line should be active only for those who have problems
		//with cloak after upgrade to 3.2
		//$ftp_server = str_replace("www.","",$domain);

		$tempfile = $filename.$extension;
		$temppath = $path.$udirectory."/".$tempfile;
		$tempfile_embed = $filename."_embed".$extension;
		$temppath_embed = $path.$udirectory."/".$tempfile_embed;		

		//what if subdomain?
		//$domain = "something.domain.com"?

		//write temporary file to adtrackz directory (it's chmod 777, so should work)
		if (!$handle = @fopen($tempfile, 'w'))
			fatal_error("Can't create $tempfile. Please make sure the \"$udirectory\" directory is chmod777. Line: ".__LINE__);
		if (@fwrite($handle, $contents) === FALSE) 
			fatal_error("Can't write to $tempfile. Line: ".__LINE__);
		fclose($handle);

		if($sticky && $encrypt)
		{
			if (!$handle = @fopen($tempfile_embed, 'w'))
				fatal_error("Can't create $tempfile_embed. Please make sure the \"$udirectory\" directory is chmod777. Line: ".__LINE__);
			if (@fwrite($handle, $contents_embed) === FALSE) 
				fatal_error("Can't write to $tempfile_embed. Line: ".__LINE__);
			fclose($handle);
		}

		//use ftp_connect, ftp_login, ftp_put to place file anywhere on server
		if (!$conn_id = @ftp_connect($ftp_server))
			fatal_error("Couldn't connect to the ftp server: $ftp_server. Line: ".__LINE__);		
		if (@ftp_login($conn_id, $user, $pass) === FALSE)
			fatal_error("Couldn't login to $ftp_server with username: $user and password: $pass. If your FTP information has changed you can modify it by selecting \"Update Profile\" in the Help menu. Line: ".__LINE__);
		if (@ftp_chdir($conn_id, $basedir) === FALSE)
			fatal_error("Couldn't change current directory to the basedir: $basedir. Line: ".__LINE__);
		if ($directory)
		{ 			
			//create directory only if it's not already there
			if (!is_dir($path.$directory)) 
				if (!@ftp_mkdir($conn_id, $directory))
					 fatal_error("Couldn't create directory $directory. Line: ".__LINE__);
			if(@ftp_chdir($conn_id, $directory) === FALSE)
				 fatal_error("Couldn't change directory to $directory. Line: ".__LINE__);
		}
		if (!@ftp_put($conn_id, $tempfile, $temppath, FTP_ASCII)) 
			fatal_error("Couldn't upload $tempfile. You may have entered the wrong server path during setup. The path you entered is: $www_path. Line: ".__LINE__);
		if($sticky && $encrypt)
			if (!@ftp_put($conn_id, $tempfile_embed, $temppath_embed, FTP_ASCII)) 
				fatal_error("Couldn't upload $tempfile_embed. You may have entered the wrong server path during setup. The path you entered is: $www_path. Line: ".__LINE__);
		ftp_close($conn_id);

		//delete tempfiles from server
		if (!@unlink($temppath))
			alert("$temppath couldn't be deleted from the $udirectory directory. This will not affect the operation of your new redirect file; however, a temporary file still exists in your adtrackz directory . If you don't want these files to pile up on your server, it is advisable that you delete them yourself. <br><br> This error shouldn't be happening, contact <a href=http://adtrackzgold.com target=_sup>support</a> if the problem persists.");
		if($sticky && $encrypt)
			if (!@unlink($temppath_embed))
				alert("$temppath_embed couldn't be deleted from the $udirectory directory. This will not affect the operation of your new redirect file; however, a temporary file still exists in your adtrackz directory . If you don't want these files to pile up on your server, it is advisable that you delete them yourself. <br><br> This error shouldn't be happening, contact <a href=http://adtrackzgold.com target=_sup>support</a> if the problem persists.");
		
		//SAVE TO DATABASE
		if ($encrypt)
			$encrypt = 1;
		if ($sticky)
			$sticky = 1;
		mysql_query("insert into adtrackz_redirects values ('','$cID','$sub','$ftpfile','$encrypt','$sticky')");

		alert ("Your new redirect file has been uploaded to your server!<br><br>Select below for your new redirect url:<br> <textarea cols=50 rows=2>$wwwfile</textarea><br><br>Click this link to test it: <a href=$wwwfile target=_redir>$wwwfile</a></p>");
	}
}


?>

<form action="cloak.php" method="post">
<table width="90%" align="center">
<tr>
	<td>Select Campaign </td>
	<td><input type=checkbox name=select_campaign> 
		<select name="cID">
		<?		
		$campaigndata = mysql_query("select * from adtrackz_campaigns order by adcode asc");
		while($campaign1 = mysql_fetch_row($campaigndata))
			echo "<option value=\"$campaign1[0]\">$campaign1[2]</option>";
		?>
		</select>
	</td>
</tr>
<tr>
	<td colspan=2>
	Or
	</td>
</tr>
<tr>
	<td>Select Split Test </td>
	<td><input type=checkbox name=select_splittest>
		<select name="st_ID">
		<?		
		$stdata = mysql_query("select * from adtrackz_split_test_groups order by name asc");
		while($st1 = mysql_fetch_row($stdata))
			echo "<option value=\"$st1[0]\">$st1[1]</option>";
		?>
		</select>
	</td>
</tr>
<tr>
	<td>Subcampaign:</td>
	<td><input type="text" name="sub" size="30" maxlength="100"></td>
</tr>
<tr>
	<td>Filename:</td>
	<td>
		<input type="text" name="filename" size="15"> 
		<select name="extension">
		<option value=".html">html</option>
		<option value=".htm">htm</option>
		</select>
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	If you enter somefile.html, then your new tracking url will be http://yourdomain.com/somefile.html.	
	</span>
	</td>
</tr>
<tr>
	<td>Directory:</td>
	<td><input type="text" name="directory" size="15"></td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	If you enter somedirectory, your tracking url will be http://yourdomain.com/somedirectory/filename.html.
	</span>
	</td>
</tr>
<tr>
	<td>Sticky</td>
	<td>
		<input type="checkbox" name="sticky">&nbsp; 
		<input type="text" name="title" size="20"> &nbsp; Title
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	If you are advertising as an affiliate, this option will keep your domain name in the address bar.
	This provides some protection against people who might otherwise strip your affiliate id from
	your ad. Enter the browser title you would like your visitor to see.
	</span>
	</td>
</tr>
<tr>
	<td>Encrypt</td>
	<td>
		<input type="checkbox" name="encrypt">&nbsp; 
		<input type="text" name="merchant_url" size="20" value="http://">&nbsp;Merchant Url
	</td>
</tr>
<tr>
	<td>&nbsp;</td>
	<td><span class=small>
	Use this option if you want to completely hide your affiliate id.
	Enter the affiliate merchant url. For example, the url
		that visitors will end up *after* clicking your affiliate link. If you are an affiliate for http://somewebsite.com and your affiliate url is http://somewebsite.com?affID=1234, then you would enter http://somewebsite.com here.
	</span>
	</td>
</tr>
<tr>
	<td colspan="2" align="center"><br><input type="submit" name="submit" value="Create Redirect"></td>
</tr>
</table>
</form>


<?

require "footer.php";
?>